#include <stdio.h>
#include "libdict.h"

void test(dict *dict)
{
    // 添加一些键值对
    dict_add(dict, "gg", "zero");
    dict_add(dict, "mm", "alisi");
    dict_add(dict, "dd", "Jack");
}

int main(int argc, char **argv)
{
    // 创建一个字典对象
    dict *dict = dict_new();

    // 添加一些键值对
    dict_add(dict, "key1", "value1");
    dict_add(dict, "key2", "value2");
    dict_add(dict, "key3", "value3");

    // 获取键对应的值
    char *value = dict_get(dict, "key2", NULL);
    printf("Value for key 'key2': %s\n", value);

    test(dict); // 没有strdup(), 只在生命周期有效
    value = dict_get(dict, "gg", NULL);
    printf("Value for key 'gg': %s\n", value);

    // 遍历key
    key_list *key_list, *tmp;
    dict_get_key_list(dict, &key_list);
    for (tmp = key_list; tmp != NULL; tmp = tmp->next)
    {
        printf("key: %s\n", tmp->key);
    }

    // 遍历key , value
    char *key, *val;
    int rank = 0;
    while (1)
    {
        rank = dict_enumerate(dict, rank, &key, &val);
        if (rank < 0)
            break;
        printf("[rank=%d] %20s: %s\n", rank, key, val ? val : "UNDEF");
    }

    // dump到屏幕
    dict_dump(dict, stdout);

    return 0;
}